<?php

namespace common\components\paymch;

/**
 * 支付操作工具
 *
 * @author emhome<emhome@163.com>
 * @since 2.0
 */
class MchUtils {

    const PREFIX_BEGIN_KEY = 'BEGIN';
    const PREFIX_END_KEY = 'END';
    const PREFIX_COMPLETION = 'KEY';

    /**
     * 获取公钥
     * @param string $path 公钥地址
     * @param string $content 公钥字符串
     * @param string $signtext 签名头内容
     * @return cert|false
     */
    public static function getPublicKey($path = null, $content = null, $signtext = 'PUBLIC') {
        if (!self::checkEmpty($path)) {
            //读取公钥文件
            $pubKey = file_get_contents($path);
            //转换为openssl格式密钥
            return openssl_get_publickey($pubKey);
        }
        if (!self::checkEmpty($content)) {
            //读取公钥串并转化成格式化内容
            return self::formatKey($content, $signtext);
        }
        return false;
    }

    /**
     * 获取私钥
     * @param string $path 私钥地址
     * @param string $content 私钥字符串
     * @param string $signtext 签名头内容
     * @return cert|false
     */
    public static function getPrivateKey($path = null, $content = null, $signtext = 'RSA PRIVATE') {
        if (!self::checkEmpty($path)) {
            //读取私钥文件
            $pubKey = file_get_contents($path);
            //转换为openssl格式密钥
            return openssl_get_privatekey($pubKey);
        }
        if (!self::checkEmpty($content)) {
            //读取私钥串并转化成格式化内容
            return self::formatKey($content, $signtext);
        }
        return false;
    }

    /**
     * 格式化数字证书字符串内容
     * @param string $key 待格式化处理的证书字符串
     * @param string $type 格式化类型
     * @return string
     */
    public static function formatKey($key, $type = 'PUBLIC') {
        $labelText = trim($type);

        $beginLabel = [static::PREFIX_BEGIN_KEY, $labelText, static::PREFIX_COMPLETION];
        $endLabel = [static::PREFIX_END_KEY, $labelText, static::PREFIX_COMPLETION];

        $begin = strtoupper(implode(' ', $beginLabel));
        $end = strtoupper(implode(' ', $endLabel));

        $header = str_pad($begin, strlen($begin) + 10, "-", STR_PAD_BOTH);
        $footer = str_pad($end, strlen($end) + 10, "-", STR_PAD_BOTH);

        $body = wordwrap($key, 64, "\n", true);
        return implode("\n", compact('header', 'body', 'footer'));
    }

    /**
     * 校验内容是否非空
     * not set is null return true
     * @param string $content 待检验内容
     * @return boolean
     */
    public static function checkEmpty($content) {
        if (!isset($content)) {
            return true;
        }
        if ($content === null) {
            return true;
        }
        if (trim($content) === "") {
            return true;
        }
        return false;
    }

    /**
     * 转换字符集编码
     * @param $data
     * @param $targetCharset
     * @param $fileType
     * @return string
     */
    public static function characet($data, $targetCharset, $fileType) {
        if (!empty($data) && strcasecmp($fileType, $targetCharset) != 0) {
            $data = mb_convert_encoding($data, $targetCharset, $fileType);
        }
        return $data;
    }

}
